<html>
<%@ include file="../../common/taglibs.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><fmt:message key="format.message.system" /></title>

<script type="text/javascript" >
//游戏管理页面
Ext.namespace('azure', 'azure.common');
azure.common.gameList = function() {
	var cm_game;
	var grid_game, store_game;
	var mainpanel;
	var toolbar_res;
	var addGameForm,editGameForm;
	var addGameWin,editGameWin;
	var rowActions;
	
	// 創建ColumnModel
	function createCM() {
		rowActions = new Ext.ux.grid.RowActions({
			actions : [{
				iconCls : 'icon-minus',
				qtip : '<fmt:message key="format.message.delete"/>'
			}, {
				iconCls : 'icon-edit-record',
				qtip : '<fmt:message key="format.message.edit"/>'
			},{
				iconCls : 'icon-play',
				qtip : '<fmt:message key="format.game.playsetting"/>'
			}, {
				iconCls : 'icon-desk',
				qtip : '<fmt:message key="format.message.desktop"/>'
			}],
				widthIntercept : Ext.isSafari ? 4 : 2,
				id : 'game-actions'
			});
		rowActions.on('action',onRowAction, this);
		
		cm_game = new Ext.grid.ColumnModel([
		{header : '<fmt:message key="format.message.no"/>',dataIndex : 'gameno',sortable : true},
		{header : '<fmt:message key="format.game.gameName"/>',dataIndex : 'gamename'}, 
		{header : '<fmt:message key="format.game.gameType"/>',dataIndex : 'type'},
		{header : '<fmt:message key="format.game.gameRule"/>',width:200,dataIndex : 'rule'},
		rowActions
		]);


	}

	function createStore() {
		store_game = new Ext.data.Store({
			id:'gameStore',
			proxy : new Ext.data.HttpProxy({
				url : 'Game_findByPage.action'
			}),
			reader : new Ext.data.JsonReader({
				root : 'pager.data',
				totalProperty : 'pager.totalSize'
			}, [
		{
			name : 'gameno',
			mapping : 'gameno'
		}, {
			name : 'gamename',
			mapping : 'gamename'
		}, {
			name : 'type',
			mapping : 'type'
		}, {
			name : 'rule',
			mapping : 'rule'
		}])
		});
	}

	function createToolbar() {
		toolbar_res = new Ext.Toolbar([{
			text : '<fmt:message key="format.title.game"/>',
			handler : addNew
		}]);
	}
	function createGrid() {
		createCM();
		createStore();
		createToolbar();

		grid_game = new Ext.grid.GridPanel({
			cm : cm_game,
			store : store_game,
			height :490,	
			plugins : [rowActions],			
			bbar : new Ext.PagingToolbar({
				grid : grid_game,
				store : store_game,
				pageSize : 15,
				displayInfo : true,
				displayMsg : '<fmt:message key="format.paging.displayMsg"/>',
				emptyMsg : '<fmt:message key="format.paging.emptyMsg"/>'
			})
		});
		//grid_game.render();
		store_game.load({params:{
			start:0,
			limit:15
		}});

	}

	function createUI() {
		createGrid();
		createForm();
		mainpanel = new Ext.Panel({
			frame : false,
			renderTo : 'gameDiv',
			items : [toolbar_res, grid_game]
		});
	}

	function createForm() {
		// 星级
		var addGameType = new Ext.form.ComboBox( {
			store : new Ext.data.SimpleStore( {
				fields : [ 'value', 'name' ],
				data : [ [ '1', '彩票' ], [ '2', '足球' ], [ '3', '3D' ]]
			}),
			fieldLabel : '<fmt:message key="format.game.gameType"/>',
			editable : false,
			hiddenName :'game.type',
			displayField : 'name',
			mode : 'local',
			triggerAction : 'all',
			maxHeight : 200,
			width : 220,
			valueField : 'value',
			selectedClass : '',
			value : '',
			selectOnFocus : true,
			autoLoad : true
		});
		
		addGameForm = new Ext.form.FormPanel({
			id:'addatention',
			labelWidth : 65,
			frame : true,
			url:'Game_create.action',
			labelSeparator : ':',
			labelAlign : 'left',
			defaultType : 'textfield',
			items : [new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.game.gameName"/>',
				width : 220,
				name : 'game.gamename'
			})
			,addGameType
			,new Ext.form.HtmlEditor({
				fieldLabel : '<fmt:message key="format.game.gameRule"/>',
				name : 'game.rule',
				width : 300,
				labelWidth : 50,
				anchor : '99%'
			})]
		});

		// 星级
		var editGameType = new Ext.form.ComboBox( {
			store : new Ext.data.SimpleStore( {
				fields : [ 'value', 'name' ],
				data : [ [ '1', '彩票' ], [ '2', '足球' ], [ '3', '3D' ]]
			}),
			fieldLabel : '<fmt:message key="format.game.gameType"/>',
			editable : false,
			hiddenName :'game.type',
			id:'type',
			name:'type',
			displayField : 'name',
			mode : 'local',
			triggerAction : 'all',
			maxHeight : 200,
			width : 220,
			valueField : 'value',
			selectedClass : '',
			value : '',
			selectOnFocus : true,
			autoLoad : true
		});
		
		editGameForm = new Ext.form.FormPanel({
			labelWidth : 65,
			frame : true,
			url:'Game_updateGame.action',
			labelSeparator : ':',
			labelAlign : 'right',
			defaultType : 'textfield',
			items : [new Ext.form.Hidden({
				id:'gameno',
				name : 'game.gameno'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.game.gameName"/>',
				width : 220,
				id : 'gamename',
				name : 'game.gamename'
			}),
			editGameType
			,new Ext.form.HtmlEditor({
				fieldLabel : '<fmt:message key="format.game.gameRule"/>',
				id : 'rule',
				name : 'game.rule',
				width : 300,
				labelWidth : 50,
				anchor : '99%'
			})]
		});
	}

	
	function addNew() {
		addGameWin = new Ext.Window({
			width : 620,
			height : 340,
			border : false,
			closable : false,
			layout : 'fit',
			items : addGameForm,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler : create,
				formBind : true
			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					addGameWin.hide();
				}
			}],
			modal : true
		});

		addGameWin.title = '<fmt:message key="format.title.game"/>';
		addGameWin.show(Ext.getBody());
	}

	//新增方法
	create = function(btn) {
		var addatention = Ext.getCmp('addatention');
		if(addatention.getForm().isValid()){
			btn.disable();
			addatention.getForm().submit({
				method : 'POST',
				waitMsg : '<fmt:message key="format.message.saving"/>',
				successProperty : false,
				success : function() {
					Ext.Msg.alert('<fmt:message key="format.message.success"/>', 
						'<fmt:message key="format.message.save.success"/>',function() {
						btn.enable();
						store_game.reload();
					});
				},
				failure : function() {
					Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
					'<fmt:message key="format.message.failure.success"/>');
					btn.enable();
				}
			});
			}
	}

	//编辑方法
	doEdit=function() {
		editGameWin = new Ext.Window({
			width : 620,
			height : 340,
			closable : false,
			layout : 'fit',
			items : editGameForm,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler: function(btn) {
					if(editGameForm.getForm().isValid()){
						btn.disable();
						editGameForm.getForm().submit({
							method : 'POST',
							waitMsg : '<fmt:message key="format.message.saving"/>',
							successProperty : false,
							success : function() {
								Ext.Msg.alert('<fmt:message key="format.message.success"/>',
									'<fmt:message key="format.message.update.success"/>',function() {
									btn.enable();
									store_game.reload();
								});
							},
							failure : function() {
								Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
									'<fmt:message key="format.message.update.failure"/>');
								btn.enable();
							}
						});
						}
				},
				formBind:true

			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					editGameWin.hide();
				}
			}],
			modal : true
		});
	}

	//删除方法
	onRowAction = function(grid, record, action, row, col) {
		switch (action) {
			case 'icon-minus' :
				Ext.Msg.confirm('<fmt:message key="format.confirm.delete.title"/>', 
					'<fmt:message key="format.confirm.delete.message"/>', function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url : 'Game_deleteGame.action',
							params : {
								'game.gameno' : record.data.gameno
							},
							success : function() {
								grid_game.getStore().remove(record);
							},
							failure : function() {
							Ext.Msg.show({
								title : '<fmt:message key="format.error.message"/>',
								msg : '<fmt:message key="format.error.delete"/>',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR
							});
							}
						});
					}
				});
			break;
			case 'icon-edit-record' :
				doEdit();
				editGameWin.setTitle('<fmt:message key="format.title.game.edit"/>');
				editGameWin.show(Ext.getBody());
				editGameForm.getForm().loadRecord(record);				
			break;
			case 'icon-play' :
				
				var gameId = record.data.gameno;
				var gameName = record.data.gamename;
				var titleName = record.data.gamename+"-->"+'<fmt:message key="format.game.playsetting"/>';
				openPage('gameSetPlay',titleName,'/newbingo/pages/gameMgr/gamePlay.jsp',gameId);

			break;
			case 'icon-desk' :
				var gameId = record.data.gameno;
				var gameName = record.data.gamename;
				var titleName = record.data.gamename+"-->"+'<fmt:message key="format.title.gameTable"/>';
				openPage('gamedesk',titleName,'/newbingo/pages/gameMgr/gameDesk.jsp',gameId);

			break;
		}
	}
	//页面初始化
	return {
		init : function() {
			createUI();
		}
	};
}();
Ext.onReady(azure.common.gameList.init, azure.common.gameList, true);
</script>
</head>

	<body>
		<div id="gameDiv"></div>
	</body>
</html>
